package softwarehouse;


import java.io.*;
import java.security.*;

public class mykeygen {
    /**
     * Generates a public and private key pair 
     * 
     * @param p location and files name of where to write public key
     * @param pr location and files name of where to write private key
     */
    public mykeygen(String p,String pr) {
        try {
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

            KeyPair pair = keyGen.generateKeyPair();
            PrivateKey priv = pair.getPrivate();
            PublicKey pub = pair.getPublic();


            byte[] key = pub.getEncoded();
            FileOutputStream keyfos = new FileOutputStream(p);
            keyfos.write(key);
            keyfos.close();

            byte[] key1 = priv.getEncoded();
            System.out.println("Private key length: " + key1.length);
            FileOutputStream keyfos1 = new FileOutputStream(pr);
            keyfos1.write(key1);
            keyfos1.close();

        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }
    }

    public static void main(String args[]) {
        mykeygen p = new mykeygen("/Users/matt/publickey/mattpk","/Users/matt/publickey/mattprk");
    }
}
